package leesoft.gocrm.servlet;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import leesoft.gocrm.models.Position;
import leesoft.gocrm.models.Positions;

import org.apache.commons.digester.Digester;
import org.apache.velocity.context.Context;

import com.google.appengine.repackaged.com.google.common.base.Log;

public class MakeContactServlet extends LayoutServletBase {
	private Positions positions; 

	@Override
	protected String createTemplatePath() {
		// TODO Auto-generated method stub
		return "makecontact.html";
	}
	protected void fillContext(Context context, HttpServletRequest request)
    {
		initPositions("/WEB-INF/positions.example.xml");
		context.put("postionsMarked", positions.getList());
		context.put("map_api_key", leesoft.gocrm.utils.PropertyManager.getInstance().getMapAPIKey());  
    }
	protected String getCurrentAction()
	{
		return "/makecontact";
	}
	protected String getCurrentLayout()
	{
		return "make_contact.html";
	}
	
	private void initPositions(String filepath)
	{
		Digester digester=new Digester();
		digester.setValidating(false);
		digester.addObjectCreate("positions",Positions.class);
		digester.addObjectCreate("positions/position", Position.class);
		digester.addBeanPropertySetter("positions/position/code","code");
		digester.addBeanPropertySetter("positions/position/lat", "latitute");
		digester.addBeanPropertySetter("positions/position/lng","longtitute");
		digester.addBeanPropertySetter("positions/position/name", "positionName");
		digester.addBeanPropertySetter("positions/position/description", "description");
		digester.addSetNext("positions/position", "addPosition");
		InputStream in=this.getServletContext().getResourceAsStream(filepath);
		try{
			positions=(Positions)digester.parse(in);
		}catch(Exception e)
		{
			Logger.global.log(Level.SEVERE, "XML file parsed error");
		}finally
		{
			if(in!=null)
			{
				try
				{
					in.close();
				}catch(Exception ex)
				{
					Logger.global.log(Level.SEVERE, "Can't close input stream");
				}
			}
		}
	}

}
